"""
Case Type   : 事件触发器
Case Name   : 【事件触发器】创建事件触发器，指定触发命令不被事件所支持，创建失败
Create At   :   2024/07/13
Owner       :   sungang14
Description :
    预置条件：
        1.创建事件触发器函数：
        CREATE OR REPLACE FUNCTION f_eventtrigger_0041()
        RETURNS event_trigger
        LANGUAGE plpgsql
        AS $$ BEGIN
        RAISE NOTICE 'test_event_trigger: % %', tg_event, tg_tag;
        END;
        $$;
    测试步骤：
        1、CREATE EVENT TRIGGER test_eventtri ON ddl_command_start WHEN TAG IN ('SELECT') EXECUTE PROCEDURE f_eventtrigger_0041();
        2、CREATE EVENT TRIGGER test_eventtri ON ddl_command_start WHEN EVENT IN ('ddl_command_start') EXECUTE PROCEDURE f_eventtrigger_0041();
        3、CREATE EVENT TRIGGER test_eventtri ON ddl_command_start WHEN TAG IN ('create table') and tag in ('drop table') EXECUTE PROCEDURE f_eventtrigger_0041();
Expect      : 
        1、创建失败，报错：filter value "SELECT" not recognized for filter variable "tag"
        2、创建失败
        3、创建失败
History     : 
"""

import sys
import os
import unittest
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH


class IndexFileDamaged(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.logger.info('----------------this is setup-----------------------')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
        self.comsh = CommonSH('PrimaryDbBmode')
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists eventtri_eventtrigger_0041_1();")
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists eventtri_eventtrigger_0041_2();")
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists eventtri_eventtrigger_0041_3();")
        self.comsh.execut_db_sql("drop function if exists f_eventtrigger_0041();")
        msg = self.comsh.execut_db_sql(
            f"CREATE OR REPLACE FUNCTION f_eventtrigger_0041() RETURNS event_trigger LANGUAGE plpgsql" \
            f" AS \$\$ BEGIN" \
            f" RAISE NOTICE 'test_event_trigger: % %', tg_event, tg_tag;" \
            f"END;" \
            f"\$\$;")
        self.logger.info(msg)
        self.assertIn("CREATE FUNCTION", msg)

    def test_Index_file_damaged(self):
        self.logger.info(
            "创建事件触发器SELECT不被事件所支持创建失败:CREATE EVENT TRIGGER eventtri_eventtrigger_0041_1 ON ddl_command_start WHEN TAG IN ('SELECT') EXECUTE PROCEDURE f_eventtrigger_0041();")
        msg = self.comsh.execut_db_sql(
            "CREATE EVENT TRIGGER eventtri_eventtrigger_0041_1 ON ddl_command_start WHEN TAG IN ('SELECT') EXECUTE PROCEDURE f_eventtrigger_0041();")
        self.logger.info(msg)
        self.assertEqual(f'ERROR:  filter value "SELECT" not recognized for filter variable "tag"', msg)

        self.logger.info(
            "创建事件触发器ddl_command_start不被事件所支持创建失败:CREATE EVENT TRIGGER eventtri_eventtrigger_0041_2 ON ddl_command_start WHEN EVENT IN ('ddl_command_start') EXECUTE PROCEDURE f_eventtrigger_0041();")
        msg = self.comsh.execut_db_sql(
            "CREATE EVENT TRIGGER eventtri_eventtrigger_0041_2 ON ddl_command_start WHEN EVENT IN ('ddl_command_start') EXECUTE PROCEDURE f_eventtrigger_0041();")
        self.logger.info(msg)
        self.assertEqual(f'ERROR:  unrecognized filter variable "event"', msg)

        self.logger.info(
            "事件触发器仅支持tag:CREATE EVENT TRIGGER eventtri_eventtrigger_0041_3 ON ddl_command_start WHEN TAG IN ('create table') and tag in ('drop table') EXECUTE PROCEDURE f_eventtrigger_0041();")
        msg = self.comsh.execut_db_sql(
            "CREATE EVENT TRIGGER eventtri_eventtrigger_0041_3 ON ddl_command_start WHEN TAG IN ('create table') and tag in ('drop table') EXECUTE PROCEDURE f_eventtrigger_0041();")
        self.logger.info(msg)
        self.assertEqual(f'ERROR:  filter variable "tag" specified more than once', msg)

    def tearDown(self):
        self.logger.info('----------------this is tearDown-----------------------')
        # 删除事件触发器函数
        msg = self.comsh.execut_db_sql("drop function if exists f_eventtrigger_0041();")
        self.logger.info(msg)
        self.assertIn('DROP FUNCTION', msg)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
